package com.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class TestZookeeper {
    public static void main(String[] args) throws Exception {
//        creaet();
        list();
        get();
        delete();
    }

    /**
     * 查询一个节点中存储的值
     */
    public static void delete() throws Exception{
        ZooKeeper zooKeeper = new ZooKeeper("localhost:2181", 1000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("链接监听");
            }
        });
        Stat stat = new Stat();
        zooKeeper.getData("/parent/sequence0000000001", false, stat);
        System.out.println(stat.getCversion());
        zooKeeper.delete("/parent/sequence0000000001", stat.getCversion());

    }


    /**
     * 查询一个节点中存储的值
     */
    public static void get() throws Exception{
        ZooKeeper zooKeeper = new ZooKeeper("localhost:2181", 1000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("链接监听");
            }
        });
        //
        byte[] data = zooKeeper.getData("/parent", false, null);
        System.out.println(new String(data));

    }

    private static void list() throws Exception {
        ZooKeeper zooKeeper = new ZooKeeper("localhost:2181", 1000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("链接监听");
            }
        });
        listAll(zooKeeper, "/");
    }
    private static void listAll(ZooKeeper zooKeeper, String path) throws Exception {
        //获取当前节点的所有子节点
        List<String> children = zooKeeper.getChildren(path, false);
        for (String child : children) {
            String currentNodeName = "/".equals(path)?path + child : (path +"/"+ child);
            System.out.println(currentNodeName);
            listAll(zooKeeper, currentNodeName);
        }
    }

    /**
     * 使用java 访问 zk
     */
    public static void creaet() throws IOException, InterruptedException, KeeperException {
        ZooKeeper zooKeeper = new ZooKeeper("127.0.0.1:2181", 1000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("zk process watch");
            }
        });;

        String s = zooKeeper.create("/parent", "test".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("creaet result "+ s);
        String s1 = zooKeeper.create("/parent/tmp", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("创建tmp 结果："+s1);

        String s2 = zooKeeper.create("/parent/sequence", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
        System.out.println("创建sequence 结果："+s2);

        Thread.sleep(10000);
        zooKeeper.close();

    }
}
